﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
using webnew.BLL;
namespace webnew.Systems
{
    public partial class S_titleEdit : WebPageBase.EditBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //设置列表页面名，即 返回的页面名
            _ListSrc = "S_titleList.aspx";
            #region 隐藏母板页面按钮
            this.Master.FindControl("btnSaveBack").Visible = true;
            this.Master.FindControl("btnSaveStay").Visible = true;
            this.Master.FindControl("btnBack").Visible = true;
            
            #endregion
            if (!IsPostBack)
            {
                //设置页面标题
                SetTitle(true, "后台管理 >> 职务管理");
                                        
            }
        }
        /// <summary>
        /// 重写基类，显示数据方法
        /// </summary>
        protected override void ShowData()
        {
            
            Model.Sys_TitleInfo model = new BLL.Sys_Title().GetList(_RecordId);
            txtName.Text = model.titlename;
            txtTitleSort.Text = model.sort.ToString();
            txtRemark.Text = model.remark;
            ViewState["currentTitleName"] = model.titlename; //保存当前职位名称,用于验证判断
        }

        /// <summary>
        /// 重写基类，传入要验证的数据
        /// </summary>
        protected override List<PageValid> _SetValidData()
        {
            List<PageValid> lst = new List<PageValid>();
            lst.Add(new PageValid("序号", txtTitleSort.Text.Replace(" ", ""), PageValid.validType.CommonValid, false, 5));
            lst.Add(new PageValid("序号", txtTitleSort.Text.Replace(" ", ""), PageValid.validType.IntValid, "只能为正整数"));
            lst.Add(new PageValid("序号", "0", txtTitleSort.Text.Replace(" ", ""), PageValid.validType.NoSameValid, "不能为0数字"));
            lst.Add(new PageValid("职位名称", txtName.Text, PageValid.validType.CommonValid, false, 25));
            //判断是否存在相同的职位名称
            if (_RecordId != null) //修改
            {
                //判断用户在修改状态下是否修改过职位名称,没修改过 则跳过验证
                if (ViewState["currentTitleName"].ToString() != txtName.Text)
                    lst.Add(new PageValid("职务名称", "1", new BLL.Sys_Title().ExistTitleName(txtName.Text), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            }
            else //添加
                lst.Add(new PageValid("职务名称", "1", new BLL.Sys_Title().ExistTitleName(txtName.Text), PageValid.validType.NoSameValid, "已存在,请重新输入"));

            return lst;
        }

        /// <summary>
        /// 重写基类保存数据方法
        /// </summary>
        protected override int SaveData()
        {
            Model.Sys_TitleInfo model = new Model.Sys_TitleInfo();
            model.Id = _RecordId;
            model.titlename = txtName.Text;
            model.Createdate = DateTime.Now;
            model.sort = MyConvert.toInt32(txtTitleSort.Text);
            model.remark = txtRemark.Text;
            model.isdel = 0;

            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息
            if (_RecordId !=null)  //修改
            {
                try
                {
                return new BLL.Sys_Title().Update(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "修改", modelLog, "系统职务", "职务，职务名称为" + txtName.Text);
                }
            }
            else                //添加
            {
               //if (txtTitleSort.Text.Trim().Equals("")) model.sort = 1; //如果序号为空，则序号默认为1              
                try { return new BLL.Sys_Title().Add(model); }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "添加", modelLog, "系统职务", "职务，职务名称为" + txtName.Text);
                }
            }

            //弹出文本框成功或者失败
            if (error == "") { return 1; }       //成功
            else { return 0; }                   //失败
        }
    }
}
